package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;

@Namespace("/")
@ParentPackage("json-default")
@Scope("propotype")
@Controller
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {
	// 添加模型驱动
	private Courier Courier = new Courier();

	@Override
	public Courier getModel() {
		return Courier;
	}

	// 注入service层
	@Resource
	private CourierService courierService;

	// 新增快递员的方法,注意:路径跳转方式为重定向
	@Action(value = "courier_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
	public String saveCourier() {
		// 将页面发送过来的快递员信息保存进数据库
		courierService.save(Courier);
		return SUCCESS;

	}

	// 用属性驱动封装page和rows
	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	// 分页查询快递员的方法
	@Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })
	public String courierPageQuery() {

		Specification<Courier> specification = new Specification<Courier>() {
			@Override
			// root就是当前要操作的主表,cb就等于多个查询条件的拼接
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 创建list集合储存,储存查询条件
				List<Predicate> li = new ArrayList<>();
				if (StringUtils.isNotBlank(Courier.getCourierNum())) {
					// 进行快递员工号查询
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), Courier.getCourierNum());
					li.add(p1);
				}
				if (StringUtils.isNotBlank(Courier.getCompany())) {
					// 进行公司模糊查询
					Predicate p2 = cb.like(root.get("company").as(String.class), "%" + Courier.getCompany() + "%");
					li.add(p2);
				}
				if (StringUtils.isNotBlank(Courier.getType())) {
					// 进行快递员类型查询
					Predicate p3 = cb.equal(root.get("type").as(String.class), Courier.getType());
					li.add(p3);
				}
				// 多表查询(查询courier中包含的standar类)
				Join<Object, Object> standartJoin = root.join("standard", JoinType.INNER);
				if (Courier.getStandard() != null && StringUtils.isNotBlank(Courier.getStandard().getName())) {
					// 进行收派标准的模糊查询
					Predicate p4 = cb.like(standartJoin.get("name").as(String.class), Courier.getStandard().getName());
					li.add(p4);
				}
				return cb.and(li.toArray(new Predicate[0]));
			}
		};
		// 将页面传回数据封装进pageable中,
		Pageable pageable = new PageRequest(page - 1, rows);
		// 调用业务层自动查询分页信息封装进page中
		Page<Courier> page = courierService.findPageData(specification, pageable);
		// 将需要返回页面的数据存入map集合中,以json的格式回写到页面
		Map<String, Object> map = new HashMap<>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		// 将集合存入值栈中
		ServletActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}

	// 还原&作废快递员的操作
	// 使用属性驱动封装数据
	private String ids;
	private Character flag;

	public void setIds(String ids) {
		this.ids = ids;
	}

	public void setFlag(Character flag) {
		this.flag = flag;
	}

	@Action(value = "courier_batchDeltag", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
	public String batchDeltag() {
		String[] idArray = ids.split(",");
		courierService.batchDeltag(idArray, flag);
		return SUCCESS;
	}

	@Action(value = "courier_noassociation", results = { @Result(name = "success", type = "json") })
	public String courierNoassociation() {
		// 调用业务层，查询未关联的快递员
		List<Courier> couriers = courierService.findNoAssociation();
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
	}
}
